package com.jokesoft.supermanager.rest;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jokesoft.supermanager.common.ReturnCode;
import com.jokesoft.supermanager.common.dto.PersonDto;
import com.jokesoft.supermanager.common.fetch.InternalFetchResponse;
import com.jokesoft.supermanager.common.save.InternalSaveResponse;
import com.jokesoft.supermanager.common.save.InternalSaveResponseImpl;
import com.jokesoft.supermanager.common.save.SavePersonDataRequest;
import com.jokesoft.supermanager.common.services.Address;
import com.jokesoft.supermanager.common.services.PersonService;

@Controller
public class SuperManagerRestController {

    private final static Logger LOGGER = LoggerFactory.getLogger(SuperManagerRestController.class);
    @Autowired
    PersonService personService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String getHome() {

        List<Address> adressList = new ArrayList<Address>();

        Address address = new Address("Hevostilantie 4 C 22", "02920", "Espoo", "Finland");

        adressList.add(address);

        Address address2 = new Address("Nuottapolku 8", "70910", "Vuorela", "Finland");

        adressList.add(address2);

        personService.addPerson("Jouko", "Hauturi", "Manager", adressList);

        return "Maybe one test person is added";
    }

    @RequestMapping(value = "/fetchPersons", method = RequestMethod.GET)
    @ResponseBody
    public InternalFetchResponse fetchPersons() {

        return null;

    }

    @RequestMapping(value = "/insertPerson", method = RequestMethod.POST)
    @ResponseBody
    public InternalSaveResponse insertPerson(@RequestBody SavePersonDataRequest request,
            HttpServletResponse servletResponse) {

        LOGGER.info("service called with request {}", request);

        InternalSaveResponse response = new InternalSaveResponseImpl();

        try {

            validateRquest(request, servletResponse);
            PersonDto person = insertPerson(request);

            response.setPersonId(person.getPersonId());
            response.setReturnCode(ReturnCode.SAVE_SUCCESS);

        } catch (MandatoryParameterMissingException e) {
            servletResponse.setStatus(HttpStatus.BAD_REQUEST.value());
            response.setReturnCode(ReturnCode.SAVE_UNSUCCESS);
            response.setErrorMessage(e.getErrorMessage());
        } catch (Exception e) {
            servletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            response.setReturnCode(ReturnCode.SAVE_UNSUCCESS);
        }

        return response;

    }

    private void validateRquest(SavePersonDataRequest request, HttpServletResponse servletResponse)
            throws MandatoryParameterMissingException {

        List<String> missingParameterList = new ArrayList<String>();

        if (request.getFirstName() == null) {
            missingParameterList.add("missing firstName");

        }

        if (request.getLastName() == null) {
            missingParameterList.add("missing lastName");
        }

        if (request.getRole() == null) {
            missingParameterList.add("missing role");
        }

        if (!missingParameterList.isEmpty()) {
            throw new MandatoryParameterMissingException(missingParameterList);
        }

    }

    private PersonDto insertPerson(SavePersonDataRequest request) {

        PersonDto person = personService.addPerson(request.getFirstName(),
                                                   request.getLastName(),
                                                   request.getRole(),
                                                   request.getAddresses());

        return person;
    }

}
